package com.lsh.oms.provider.service.order;

import com.alibaba.dubbo.config.annotation.Service;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.service.order.IOrderRestService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.tool.Criteria;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Map;

@Path("orders")
@Service(protocol = "rest", timeout = 30000)
public class OrderRestServiceImpl implements IOrderRestService {

    @Resource
    private OrderQueryService orderQueryService;

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult<Object> get(@PathParam("id") Long id,
                                    @HeaderParam("venderId") Long venderId,
                                    @QueryParam("ext-required") @DefaultValue(value = "true") boolean extRequired,
                                    @QueryParam("details-required") @DefaultValue(value = "true") boolean detailsRequired) {
        OrderHead order = orderQueryService.findByCode(id, extRequired, detailsRequired);
        if (order == null || venderNotMatch(venderId, order)) {
            throw new NotFoundException();
        }
        return buildResponseEntity(order);
    }

    private CommonResult<Object> buildResponseEntity(OrderHead order) {
        CommonResult<Object> responseEntity = new CommonResult<>();
        responseEntity.setCode(ReturnCode.REQUEST_SUCCESS);
        responseEntity.setData(order);
        return responseEntity;
    }

    private boolean venderNotMatch(Long venderId, OrderHead order) {
        return (venderId == null) || !venderId.equals(order.getVenderId());
    }

    @GET
    @Path("{id}/returnable")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public Response returnable(@PathParam("id") Long id) {
        return Response.ok(orderQueryService.returnable(id)).build();
    }

    @GET
    @Path("{id}/relations")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public Map relations(@PathParam("id") Long id,
                         @Context UriInfo uriInfo) {
        return orderQueryService.relations(id, Criteria.of(uriInfo));
    }

    @GET
    @Path("{id}/not-sign")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public Map findNotSign(@PathParam("id") Long id) {
        return orderQueryService.findNotSign(id);
    }

    @POST
    @Path("{id}/refund-notify")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public Response refundNotify(@PathParam("id") Long id, Map<String, Object> request) {
        orderQueryService.refundNotify(id, request);
        return Response.ok(CommonResult.success()).build();
    }
}
